package com.hirain.templatemethod;

/**
 * Created by caicf on 2016/8/23.
 */
public abstract class AbstractClass {

    public final void templateMethod() {
        boolean hook = hook();
        String op2 = primitiveOperation2();
        String op1=null;
        if (hook) {    //使用钩子控制是否执行primitiveOperation1()
            op1 = primitiveOperation1();
        }
        System.out.println("执行操作2后执行操作1:" + op2 + " >>> " + op1);
    }

    protected abstract String primitiveOperation1();

    protected abstract String primitiveOperation2();

    //定义钩子，子类可以选择是否覆盖这个方法
    boolean hook() {
        return true;
    }
}
